package de.htwg_konstanz.infen.aufg3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;

import org.dom4j.DocumentException;

public class MainClass {

	private static String XML_INPUT_PATH = "";
	private static String XML_OUTPUT_PATH = "";
	private static String XSD_FILE = "";

	public static void main(String[] args) {

		System.out.println("********** DataGuide Creator **********");
		System.out.println();

		if (args.length != 0) {
			if (args[0].equals("--debug") || args[0].equals("-debug")) {
				XSD_FILE = "xml" + File.separator + "xmlDataGuide.xsd";
				XML_OUTPUT_PATH = "xml" + File.separator + "output.xml";
				XML_INPUT_PATH = "xml" + File.separator + "catalog1.xml";
			}

			if (args.length == 3) {
				XML_INPUT_PATH = args[0].toString();
				XML_OUTPUT_PATH = args[1].toString();
				XSD_FILE = args[2].toString();
			}
		}
		File xmlInFile = new File(XML_INPUT_PATH);
		File xsdInFile = new File(XSD_FILE);

		if (!xmlInFile.exists() || !xsdInFile.exists()) {
			System.out.println("Please enter valid file paths.");
			System.exit(-1);
		}

		System.out.println("Input XML file: " + xmlInFile.getAbsolutePath());
		System.out.println("Input Schema file: " + xsdInFile.getAbsolutePath());
		System.out.println();

		HashMap<String, CustomHashMapValueItem> elementMap = new HashMap<String, CustomHashMapValueItem>();

		try {
			elementMap = CreateDataGuideHelper.INSTANCE.getDataGuide(new File(
					XML_INPUT_PATH));
			elementMap = DefineOccurenceHelper.INSTANCE.defineOccurence(
					XML_INPUT_PATH, elementMap);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println("DataGuide:");
		System.out.println();

		if (elementMap != null) {
			String[] splittArray = elementMap.toString().split(",");
			for (int i = 0; i < splittArray.length; i++) {
				System.out.println(splittArray[i]);
			}
		}

		System.out.println();

		CreateOutput co = new CreateOutput(XSD_FILE, XML_OUTPUT_PATH);
		co.go(elementMap);

	}

}